<template>
  <addEditBaseAchievementAnalysisSubjectView
    ref="addEditBaseAchievementAnalysisSubjectView"
  />
  <addFileView ref="addFileView" />
  <el-main>
    <div class="teacher">
      <div class="app-table">
        <div class="app-body">
          <el-form :inline="true" class="demo-form-inline" v-model="form">
            <el-form-item
              label="所在年级"
              :label-width="formLabelWidth"
              prop="gradeId"
            >
              <el-select
                v-model="form.gradeId"
                autocomplete="off"
                @change="filterGradeId"
                placeholder="请选择所在年级"
                style="width:190px;"
              >
                <el-option
                  v-for="gradeId in gradeIds"
                  :key="gradeId"
                  :value="gradeId"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item
              label="所在学年"
              :label-width="formLabelWidth"
              prop="classId"
            >
              <el-select
                v-model="form.schoolYear"
                autocomplete="off"
                placeholder="请选择所在学年"
                style="width:190px;"
              >
                <el-option
                  v-for="item in schoolYears"
                  :key="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="考试名称"
              :label-width="formLabelWidth"
              prop="classId"
            >
              <el-select
                v-model="form.typeName"
                autocomplete="off"
                placeholder="请选择考试名称"
                style="width:190px;"
              >
                <el-option
                  v-for="item in types"
                  :key="item.name"
                  :value="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="科目"
              :label-width="formLabelWidth"
              prop="subjectName"
            >
              <el-select
                v-model="form.subjectName"
                autocomplete="off"
                placeholder="请选择科目"
                style="width:190px;"
              >
                <el-option
                  v-for="item in subjectNames"
                  :key="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                plain
                @click="onSubmit"
                @keyup.enter="searchEnter"
                >搜索</el-button
              >
              <!-- <el-button
                type="warning"
                plain
                @click="editbaseAchievement"
                @keyup.enter="searchEnter"
                >一键成绩分析</el-button
              > -->
              <el-button type="success" plain @click="exportToExcel"
                >导出数据</el-button
              >
            </el-form-item>
          </el-form>

          <el-table
            size="small"
            :data="list"
            ref="table"
            style="width: 100%"
            class="custom-table"
            border
            :header-cell-style="{
              'text-align': 'center',
              'border-color': '#444444',
            }"
            :cell-style="{ 'text-align': 'center', 'border-color': '#444444' }"
          >
            <el-table-column prop="id" type="index" label="序号" width="45" />
            <!-- <el-table-column prop="name" label="批次名次" /> -->
            <!-- <el-table-column prop="modeSubject" label="选科类型" /> -->
            <!-- <el-table-column prop="teacherName" label="班主任" /> -->
            <!-- <el-table-column prop="teacherNumber" label="人员编号" /> -->
            <!-- <el-table-column prop="schoolYear" label="学年" /> -->
            <!-- <el-table-column prop="typeName" label="考试名称" /> -->
            <el-table-column
              prop="subjectName"
              label="科目"
              width="45"
              sortable
            />
            <el-table-column prop="gradeId" width="46" label="年级" />
            <el-table-column prop="classId" width="45" label="班级" sortable />
            <el-table-column
              prop="countStudent"
              width="66"
              label="统考人数"
              sortable
            />
            <el-table-column label="学科">
              <el-table-column width="70" sortable prop="percentageSubjectContribution" label="贡献率">
                <template #default="scope">
                  {{ scope.row.percentageAbove985 }}%
                </template>
              </el-table-column>
              <el-table-column
                width="55"
                sortable
                prop="rankingSubjectContribution"
                label="排名"
              />
            </el-table-column>
            <el-table-column label="班级">
              <el-table-column
                prop="percentageClassContribution"
                width="70px"
                sortable
                label="贡献率"
              >
                <template #default="scope">
                  {{ scope.row.percentageClassContribution }}%
                </template>
              </el-table-column>
              <el-table-column
                width="55"
                sortable
                prop="rankingClassContribution"
                label="排名"
              />
            </el-table-column>
            <!-- <el-table-column prop="campus" label="校区" /> -->
            <el-table-column label="985">
              <el-table-column width="55" sortable prop="above985" label="指标" />
              <el-table-column width="55" sortable prop="countAbove985" label="完成数" />
              <el-table-column width="70" sortable prop="percentageAbove985" label="贡献率">
                <template #default="scope">
                  {{ scope.row.percentageAbove985 }}%
                </template>
              </el-table-column>
            </el-table-column>
            <!-- <el-table-column prop="above985" label="985分数线" /> -->
            <el-table-column label="211">
              <el-table-column width="55" sortable prop="above211" label="指标" />
              <el-table-column width="55" sortable prop="countAbove211" label="完成数" />
              <el-table-column width="70" sortable prop="percentageAbove211" label="贡献率">
                <template #default="scope">
                  {{ scope.row.percentageAbove211 }}%
                </template>
              </el-table-column>
              <!-- <el-table-column prop="above211" label="211分数线" /> -->
            </el-table-column>
            <el-table-column label="重本">
              <el-table-column width="55" sortable prop="aboveZd" label="指标" />
              <el-table-column width="55" sortable prop="countAboveZd" label="完成数" />
              <el-table-column width="70" sortable prop="percentageAboveZd" label="贡献率">
                <template #default="scope">
                  {{ scope.row.percentageAboveZd }}%
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="一本">
              <el-table-column  width="44" sortable prop="aboveYb" label="指标" />
              <el-table-column width="55" sortable prop="countAboveYb" label="完成数" />
              <el-table-column width="70" sortable prop="percentageAboveYb" label="贡献率">
                <template #default="scope">
                  {{ scope.row.percentageAboveYb }}%
                </template>
              </el-table-column>

              <!-- <el-table-column prop="aboveYb" label="一本分数线" /> -->
            </el-table-column>
            <el-table-column label="总分完成数">
              <el-table-column width="44" sortable prop="countTotal985" label="985" />
              <el-table-column width="44" sortable prop="countTotal211" label="211" />
              <el-table-column width="44" sortable prop="countTotalZd" label="重本" />
              <el-table-column width="44" sortable prop="countTotalYb" label="一本" />
            </el-table-column>
            <el-table-column label="单科双上线">
              <el-table-column width="44" sortable prop="countDouble985" label="985" />
              <el-table-column width="44" sortable prop="countDouble211" label="211" />
              <el-table-column width="44" sortable prop="countDoubleZd" label="重本" />
              <el-table-column width="44" sortable prop="countDoubleYb" label="一本" />
            </el-table-column>
            <el-table-column label="班级贡献率">
              <el-table-column width="70" sortable prop="percentageClass985" label="985" >
                <template #default="scope">
                  {{ scope.row.percentageClass985 }}%
                </template>
              </el-table-column>
              <el-table-column width="70" sortable prop="percentageClass211" label="211" >
                <template #default="scope">
                  {{ scope.row.percentageClass211 }}%
                </template>
              </el-table-column>
              <el-table-column width="70" sortable prop="percentageClassZd" label="重本" >
                <template #default="scope">
                  {{ scope.row.percentageClassZd }}%
                </template>
              </el-table-column>
              <el-table-column width="70" sortable prop="percentageClassYb" label="一本" >
                <template #default="scope">
                  {{ scope.row.percentageClassYb }}%
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="平均贡献率">
              <el-table-column width="70" sortable prop="percentageAverage985" label="985">
                <template #default="scope">
                  {{ scope.row.percentageAverage985 }}%
                </template>
              </el-table-column>
              <el-table-column width="70" sortable prop="percentageAverage211" label="211">
                <template #default="scope">
                  {{ scope.row.percentageAverage211 }}%
                </template>
              </el-table-column>
              <el-table-column width="70" sortable prop="percentageAverageZd" label="重本">
                <template #default="scope">
                  {{ scope.row.percentageAverageZd }}%
                </template>
              </el-table-column>
              <el-table-column width="70" sortable prop="percentageAverageYb" label="一本">
                <template #default="scope">
                  {{ scope.row.percentageAverageYb }}%
                </template>
            </el-table-column>
          </el-table-column>

            <!-- <el-table-column
              prop="percentageClassContribution"
              label="班级贡献率"
            /> -->

            <!-- <el-table-column prop="rankingClassContribution" label="班级排名" /> -->
            <!-- <el-table-column prop="createTime" label="创建时间" /> -->
            <el-table-column  width="120"  prop="calculationMethod" label="计算办法" />
            <!-- <el-table-column label="操作" width="140px">
              <template style="display: block" #default="scope">
                <el-button
                  size="small"
                  type="success"
                  @click="editBaseAchievementAnalysisSubject(scope.row)"
                  >编辑
                </el-button>
                <el-button
                  size="small"
                  type="danger"
                  @click="deleteBaseAchievementAnalysisSubject(scope.row.id)"
                  >删除
                </el-button>
              </template>
            </el-table-column> -->
            <template v-slot:empty>
              <el-empty description="暂无数据"></el-empty>
            </template>
          </el-table>
          <el-config-provider :locale="locale">
            <el-pagination
              :total="total"
              :page-size="limit"
              :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="changePageSize"
              @current-change="changeCurrentPage"
            >
            </el-pagination>
          </el-config-provider>
        </div>
        <bottomView></bottomView>
      </div>
    </div>
  </el-main>
</template>
<script>
import addEditBaseAchievementAnalysisSubjectView from "@/components/achievementAnalysis/addEditBaseAchievementAnalysisSubject.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import * as XLSX from 'xlsx';
import { saveAs } from 'file-saver';
import bottomView from "@/components/bottom.vue";

export default {
  name: "BaseAchievementAnalysisSubjectListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      pageSize: 20,
      total: 0,
      limit: 20,
      list: [],
      gradeIds: [],
      types: [],
      schoolYears: [],
      subjectNames: [
        "语文",
        "数学",
        "英语",
        "物理",
        "化学",
        "生物",
        "历史",
        "地理",
        "政治",
      ],
      form: {
        name: "",
        modeSubject: "",
        subjectName: "",
        teacherName: "",
        teacherNumber: "",
        schoolYear: "",
        typeName: "",
        calculationMethod: "",
        gradeId: "",
        classId: "",
        schoolYear: "",
        typeName: "",
      },
    };
  },
  components: {XLSX,saveAs,
    addEditBaseAchievementAnalysisSubjectView,
    addFileView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload,bottomView,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.getGradeIds();
    this.getBaseAchievementAnalysisSubjectList();
  },
  watch: {
    $route() {
      this.getBaseAchievementAnalysisSubjectList();
    },
  },
  methods: {
    exportToExcel() {
      const worksheet = XLSX.utils.table_to_sheet(this.$refs.table.$el);
      // Apply cell styles
      const range = XLSX.utils.decode_range(worksheet["!ref"]);
      for (let R = range.s.r; R <= range.e.r; ++R) {
        for (let C = range.s.c; C <= range.e.c; ++C) {
          const cellAddress = XLSX.utils.encode_cell({ r: R, c: C });
          const cell = worksheet[cellAddress];
          if (cell) {
            cell.s = {
              alignment: { horizontal: "center" },
              border: {
                top: { style: "thin", color: { rgb: "FF0000" } },
                bottom: { style: "thin", color: { rgb: "FF0000" } },
                left: { style: "thin", color: { rgb: "FF0000" } },
                right: { style: "thin", color: { rgb: "FF0000" } },
              },
            };
          }
        }
      }
      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, "人员贡献率分项数据");
      const excelBuffer = XLSX.write(workbook, {
        bookType: "xlsx",
        type: "array",
      });
      const data = new Blob([excelBuffer], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
      });
      saveAs(
        data,
        this.form.gradeId +
          "级" +
          this.form.schoolYear +
          this.form.typeName +
          "人员贡献率分项数据.xlsx"
      );
    },
    editbaseAchievement(obj) {
      ElMessageBox.confirm("确定要执行成绩分析吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let formData = new FormData();
          formData.append("schoolYear", this.form.schoolYear);
          formData.append("typeName", this.form.typeName);
          formData.append("gradeId", this.form.gradeId);
          httpService(
            "/javaApi/baseAchievementAnalysis/querySubjectFinishPercentage",
            formData,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.getEBookList();
            } else {
              ElMessage.error("执行失败，请重试！");
            }
          });
        })
        .catch(() => {});
    },

    getGradeIds() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let gradeIds = [];
      if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
      } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
      }
      this.gradeIds = gradeIds;
      this.form.gradeId = gradeIds[0];
      this.getSchoolYears();
      this.getAchievementTypeList();
    },
    getAchievementTypeList() {
      httpService("/javaApi/achievementType/list/1/1000", {}, "get").then(
        (data) => {
          this.types = data.list;
          this.form.typeName = this.types[0].name;
        }
      );
    },
    getSchoolYears() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let schoolYears = [];
      if (nowMonth > 6) {
        // 2023
        schoolYears.push(nowYear - 2000 + "-" + (nowYear - 1999) + "学年上");
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年下");
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年上");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年下");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年上");
      } else {
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年下");
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年上");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年下");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年上");
        schoolYears.push(nowYear - 2003 + "-" + (nowYear - 2002) + "学年下");
        schoolYears.push(nowYear - 2003 + "-" + (nowYear - 2002) + "学年上");
      }
      this.schoolYears = schoolYears;
      this.form.schoolYear = schoolYears[0];
    },
    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getBaseAchievementAnalysisSubjectList();
      }
    },
    add() {
      this.$refs.addEditBaseAchievementAnalysisSubjectView.title = "添加";
      this.$refs.addEditBaseAchievementAnalysisSubjectView.personnelType =
        this.type;
      this.$refs.addEditBaseAchievementAnalysisSubjectView.dialogFormVisible = true;
    },
    editBaseAchievementAnalysisSubject(obj) {
      this.$refs.addEditBaseAchievementAnalysisSubjectView.title = "修改信息";
      this.$refs.addEditBaseAchievementAnalysisSubjectView.getBaseAchievementAnalysisSubjectById(
        obj.id
      );
      this.$refs.addEditBaseAchievementAnalysisSubjectView.dialogFormVisible = true;
    },
    deleteBaseAchievementAnalysisSubject(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/baseAchievementAnalysisSubject/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getBaseAchievementAnalysisSubjectList();
            } else {
              ElMessage.error("删除失败，请重试！");
            }
          });
        })
        .catch(() => {});
    },
    reloadData() {
      this.page = 1;
      this.getBaseAchievementAnalysisSubjectList();
    },
    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量导入";
      this.$refs.addFileView.uploadApiUrl =
        "/javaApi/baseAchievementAnalysisSubject/importExcel";
    },
    exportExcel() {
      httpService(
        "/javaApi/baseAchievementAnalysisSubject/exportExcel",
        {
          name: this.form.name,
          modeSubject: this.form.modeSubject,
          subjectName: this.form.subjectName,
          teacherName: this.form.teacherName,
          teacherNumber: this.form.teacherNumber,
          schoolYear: this.form.schoolYear,
          typeName: this.form.typeName,
          calculationMethod: this.form.calculationMethod,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.msg, "_blank");
        }
      });
    },
    onSubmit() {
      this.getBaseAchievementAnalysisSubjectList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getBaseAchievementAnalysisSubjectList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getBaseAchievementAnalysisSubjectList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getBaseAchievementAnalysisSubjectList() {
      httpService(
        "/javaApi/baseAchievementAnalysisSubject/list/" +
          this.page +
          "/" +
          this.pageSize,
        {
          name: this.form.name,
          modeSubject: this.form.modeSubject,
          subjectName: this.form.subjectName,
          teacherName: this.form.teacherName,
          teacherNumber: this.form.teacherNumber,
          schoolYear: this.form.schoolYear,
          typeName: this.form.typeName,
          calculationMethod: this.form.calculationMethod,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
  },
};
</script>
<style scoped>
.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 10px;
}

.app-search {
  padding-top: 20px;
  border-top: 1px solid #dedede;
  margin-top: 20px;
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 243px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}

.custom-table {
  border: 1px solid #444444; /* Set the border color for the el-table */
}
</style>

